﻿
namespace AppShare.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using AppShare.Data.Models;


    // TODO: Create methods containing your application logic.
    [EnableClientAccess()]
    public class ShareService : DomainService
    {
        #region Expose!
        public IEnumerable<SubmitData> GetSubmitFiles()
        {
            throw new NotSupportedException();
        }
        //TODO: Delete this reference! Use ShipData instead!
        public IEnumerable<Item> GetItems()
        {
            throw new NotSupportedException();
        }
        #endregion

        [Invoke(HasSideEffects = true)]
        public Item VerifyShip(SubmitData data)
        {
            if (data == null)
                throw new ArgumentNullException("SubmitData is null");

            using (var dc = MSDNEntities.Create())
            {
                var e = dc.Items.FirstOrDefault(
                            p => p.SHA1 == data.SHA1
                                && p.FileID == data.eMuleHash
                                && p.FileSize == data.FileSize);

                if (e == null)
                {
                    if (dc.SubmitFiles.FirstOrDefault(
                        p => p.SHA1 == data.SHA1
                            && p.FileID == data.eMuleHash
                            && p.FileSize == data.FileSize) == null)
                    {
                        // Design Defect: need to add a primary key to DB
                        //
                        //dc.SubmitFiles.AddObject(data.ToSubmitFile());
                        //dc.SaveChanges();
                    }
                }

                return e;
            }
        }

        public IEnumerable<ProductData> GetProducts()
        {
            //#if DEBUG
            //            System.Threading.Thread.Sleep(500);
            //#endif

            using (var dc = MSDNEntities.Create())
            {
                return (from t in dc.ProductTypes
                        orderby t.ProductType1
                        select new ProductData
                        {
                            ProductId = (int)t.ID,
                            ProductName = t.ProductType1,
                            ProductCount = (dc.ProductNames.Count(p => p.ProductTypeID == t.ID)),
                        }).ToArray();
            }
        }

        public IEnumerable<FamilyData> GetFamilies(int productId)
        {
            //#if DEBUG
            //            System.Threading.Thread.Sleep(500);
            //#endif

            using (var dc = MSDNEntities.Create())
            {
                return (from t in dc.ProductNames
                        where t.ProductTypeID == productId
                        orderby t.ProductName1
                        select new FamilyData
                        {
                            FamilyId = (int)t.ID,
                            FamilyName = t.ProductName1,
                        }).ToArray();
            }
        }

        public IEnumerable<ShipData> GetShips(int familyId)
        {
            //#if DEBUG
            //            System.Threading.Thread.Sleep(500);
            //#endif

            using (var dc = MSDNEntities.Create())
            {
                var source = (from t in dc.Items
                              where t.ProductNameID == familyId
                              select t).ToArray();


                return (from t in source
                        select ShipData.FromItem(t)).ToArray();
            }
        }
    }
}


